﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Odyssey.Controls;
using Odyssey.Controls.Classes;
using System.Windows.Controls.Primitives;
using WpfELearningAuthoringTool;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using ScormModel;
using ScormModel.View;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using ScormModel.HtmlBuilder;
using System.Collections.ObjectModel;
using ScormModel.Question;
namespace WpfELearningAuthoringTool
{
    /// <summary>
    /// Interaction logic for MultipleChoiceQuestionDialog.xaml
    /// </summary>
    public partial class MultipleChoiceQuestionDialog : Window
    {
       
        RibbonBarRegion.RibbonBarHeader parentChild;
        ObservableCollection<QuestionAnswer> listAnswer = new ObservableCollection<QuestionAnswer>();
        string currentDirectory;
        WebBrowser browser;
        ImsManifest imsmanifest;
        bool isAddNew = true;
        QuestionTest questionTest;
        public QuestionTest QuestionTest
        {
            get { return this.questionTest; }
            set { this.questionTest = value; }
        }
        public bool IsAddNew
        {
            get { return this.isAddNew; }
            set { this.isAddNew = value; }
        }
      
        public string CurrentDirectory
        {
            get { return this.currentDirectory; }
            set { this.currentDirectory = value; }
        }
        public ImsManifest Imsmanifest
        {
            get { return this.imsmanifest; }
            set { this.imsmanifest = value; }
        }
        public WebBrowser WebBrowser
        {
            get { return this.browser; }
            set { this.browser = value; }
        }
        public RibbonBarRegion.RibbonBarHeader ParentChild
        {
            get { return this.parentChild; }
            set { this.parentChild = value; }
        }
        public MultipleChoiceQuestionDialog()
        {
            InitializeComponent();
            listPossibleAnwser.ItemsSource = listAnswer;
        }

        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
           
        }
        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            QuestionAnswer qa = new QuestionAnswer();
            qa.Id = (listAnswer.Count + 1).ToString();
            qa.Answer = "Answer " + (listAnswer.Count + 1).ToString();
            qa.Correct = false;
            listAnswer.Add(qa);
        }

        private void btnRemove_Click(object sender, RoutedEventArgs e)
        {
            listAnswer.RemoveAt(listPossibleAnwser.SelectedIndex);
            List<QuestionAnswer> law = new List<QuestionAnswer>();
            for (int i = 0; i < listAnswer.Count; i++)
            {
                QuestionAnswer qi = listAnswer[i];
                qi.Id = (i + 1).ToString();
                law.Add(qi);
            }
            listAnswer.Clear();
            for (int i = 0; i < law.Count; i++)
            {
                listAnswer.Add(law[i]);
            }
        }
        private bool checkValidAnswer()
        {
            bool result = false;
            for (int i = 0; i < listAnswer.Count; i++)
            {
                QuestionAnswer qi = listAnswer[i];
                if (qi.Correct == true)
                {
                    result = true;
                    return result;
                }
                else
                    continue;
            }
            return result;
        }
        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            if (checkValidAnswer())
            {
                if (isAddNew == false) {
                    ChoiceQuestionData singleChoiceQuestion = new ChoiceQuestionData();
                    singleChoiceQuestion.QuestionContent = new TextRange(txtQuestionContent.Document.ContentStart, txtQuestionContent.Document.ContentEnd).Text;
                    singleChoiceQuestion.QuestionHint = new TextRange(txtQuestionHint.Document.ContentStart, txtQuestionHint.Document.ContentEnd).Text;
                    singleChoiceQuestion.ListPossibleAnswer = new List<QuestionAnswer>();
                    for (int i = 0; i < listAnswer.Count; i++)
                    {
                        QuestionAnswer qi = listAnswer[i];
                        singleChoiceQuestion.ListPossibleAnswer.Add(qi);
                    }
                    singleChoiceQuestion.Type = "multiple";
                    singleChoiceQuestion.Identifier = "item";
                    singleChoiceQuestion.CurrentDirectory = currentDirectory;
                    questionTest.QuestionData = singleChoiceQuestion;
                    Close();
                }
                else
                {
                    try
                    {
                        ChoiceQuestionData singleChoiceQuestion = new ChoiceQuestionData();
                        singleChoiceQuestion.QuestionContent = new TextRange(txtQuestionContent.Document.ContentStart, txtQuestionContent.Document.ContentEnd).Text;
                        singleChoiceQuestion.QuestionHint = new TextRange(txtQuestionHint.Document.ContentStart, txtQuestionHint.Document.ContentEnd).Text;
                        singleChoiceQuestion.ListPossibleAnswer = new List<QuestionAnswer>();
                        for (int i = 0; i < listAnswer.Count; i++)
                        {
                            QuestionAnswer qi = listAnswer[i];
                            singleChoiceQuestion.ListPossibleAnswer.Add(qi);
                        }
                        singleChoiceQuestion.Type = "multiple";
                        singleChoiceQuestion.Identifier = "item";
                        singleChoiceQuestion.CurrentDirectory = currentDirectory;
                        HtmlDocument content = singleChoiceQuestion.CreateHtmlNode();
                        //using (StreamWriter writer = File.CreateText(currentDirectory + "/" + "Test1.htm"))
                        //{
                        //    writer.Write(content.HTML);
                        //    writer.Close();
                        //    browser.NavigateToString(content.HTML);
                        //}
                        browser.NavigateToString(content.HTML);
                        Close();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }

                }

            }
            else
            {
                MessageBox.Show("Please Choice Answer for quesion");
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void btnHelp_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}
